﻿/**
 * Created by rain on 2016/1/25.
 */

'use strict';
/*jslint node:true*/
const path = require('path');
/*这种以CommonJS的同步形式去引入其它模块的方式代码更加简洁:获取组件*/
const os = require('os');
const moment = require('moment');
const args = require('args');
const dev = process.env.NODE_ENV == 'development';
dev && console.log('\x1B[33m%s\x1b[0m', '请遵循并及时更新 readme.md,维护良好的开发环境,媛猿有责');
// // 启动参数
args.option(['p', 'port'], '启动端口');
args.option(['u', 'api-url'], 'webapi的URL');
args.option('qnak', 'qiniuAccessKey');
args.option('qnsk', 'qiniuSecretKey');
args.option('qnbkt', 'qiniuBucket');
args.option('qndmn', 'qiniuDomain');
const flags = dev ? args.parse(process.argv) : {};

const FS_UNIAPP_API = process.env.FS_UNIAPP_API || flags.apiUrl;

const ANXINCLOUD_QINIU_ACCESSKEY = process.env.ANXINCLOUD_QINIU_ACCESSKEY || flags.qnak;
const ANXINCLOUD_QINIU_SECRETKEY = process.env.ANXINCLOUD_QINIU_SECRETKEY || flags.qnsk;
const ANXINCLOUD_QINIU_BUCKET_RESOURCE = process.env.ANXINCLOUD_QINIU_BUCKET_RESOURCE || flags.qnbkt;
const ANXINCLOUD_QINIU_DOMAIN_QNDMN_RESOURCE = process.env.ANXINCLOUD_QINIU_DOMAIN_QNDMN_RESOURCE || flags.qndmn;
const weatherUrl = process.env.ANXINCLOUD_WEATHERAPP_API || "https://weatherssj.anxinyun.cn/weatherApp/weather/getImmeData"
const BAIDU_API = process.env.BAIDU_API || "https://jiaotong.baidu.com"

//阿里OSS
const ALI_OSS_ACCESSKEY = process.env.ALI_OSS_ACCESSKEY || flags.aliOssAccessKey;
const ALI_OSS_SECRETKET = process.env.ALI_OSS_SECRETKET || flags.aliOssSecretKey;
const ALI_OSS_BUCKET = process.env.ALI_OSS_BUCKET || flags.aliOssBucket;
const ALI_OSS_REGION = process.env.ALI_OSS_REGION || flags.aliOssRegion;

if (
   !FS_UNIAPP_API
   // || !ANXINCLOUD_QINIU_ACCESSKEY
   // || !ANXINCLOUD_QINIU_SECRETKEY
   // || !ANXINCLOUD_QINIU_BUCKET_RESOURCE
   // || !ANXINCLOUD_QINIU_DOMAIN_QNDMN_RESOURCE
) {
   console.log('缺少启动参数,异常退出');
   args.showHelp();
   process.exit(-1);
}

const product = {
   port: flags.port || 3000,
   staticDirs: [path.join(__dirname, './client')],
   mws: [
      {
         entry: require('./middlewares/proxy').entry,
         opts: {
            host: FS_UNIAPP_API,
            match: /^\/_api\//,
         }
      },
      // {
      //     entry: require('./middlewares/attachment').entry,
      //     opts: {
      //         qiniu: {
      //             accessKey: ANXINCLOUD_QINIU_ACCESSKEY,
      //             secretKey: ANXINCLOUD_QINIU_SECRETKEY,
      //             bucket: ANXINCLOUD_QINIU_BUCKET_RESOURCE,
      //             domain: ANXINCLOUD_QINIU_DOMAIN_QNDMN_RESOURCE
      //         },
      //         maxSize: 104857600, // 100M
      //         uploadPath: 'other'
      //     }
      // },
      {
         entry: require('./routes').entry,
         opts: {
            apiUrl: FS_UNIAPP_API,
            staticRoot: './client',
            qiniu: {
               fetchUrl: '/_file-server',
               domain: ANXINCLOUD_QINIU_DOMAIN_QNDMN_RESOURCE
            },
            aliOss: {
               fetchUrl: '/_file-ali-server',
               accessKey: ALI_OSS_ACCESSKEY,
               secretKey: ALI_OSS_SECRETKET,
               bucket: ALI_OSS_BUCKET,
               region: ALI_OSS_REGION
            },
            weatherUrl,
            baiDuAPi: BAIDU_API
         }
      },
      {
         entry: require('./client').entry,// 静态信息
         opts: {}
      }
   ],
   logger: {
      level: 'debug',
      json: false,
      filename: path.join(__dirname, 'log', 'runtime.txt'),
      colorize: true,
      maxsize: 1024 * 1024 * 5,
      rotationFormat: false,
      zippedArchive: true,
      maxFiles: 10,
      prettyPrint: true,
      label: '',
      timestamp: () => moment().format('YYYY-MM-DD HH:mm:ss.SSS'),
      eol: os.EOL,
      tailable: true,
      depth: null,
      showLevel: true,
      maxRetries: 1
   }
};
if (product.frontParams) {
   const patt = /[^A-Z|_]+/
   for (let k in product.frontParams) {
      if (!k.startsWith('FS_') || patt.test(k)) {
         console.warn(`参数 ${k} 当以大写字母和下划线组成 且以 FS_ 开头`);
         process.exit(-1);
      }
   }
}

let config;
if (dev) {
   config = {
      port: product.port,
      staticDirs: product.staticDirs,
      frontParams: product.frontParams,
      mws: product.mws.concat([
         {
            entry: require('./middlewares/webpack-dev').entry,
            opts: {
               frontParams: product.frontParams,
            }
         }
      ]),
      logger: product.logger
   }
   config.logger.filename = path.join(__dirname, 'log', 'development.txt');
} else {
   config = product;
}

module.exports = config;//区分开发和发布